{extend name="../../admin/view/main"}

{block name="style"}{include file='gasent/form-style'}{/block}

{block name='content'}
<style>
    .upload-image-box {
        width: 130px;
        height: 110px;
        border: 1px solid rgba(125, 125, 125, .1);
        background-size: cover;
    }
    .upload-file-box {
        width: 80px;
        height: 50px;
        border: 1px solid rgba(125, 125, 125, .1);
        background-size: cover;
    }


</style>
<div id="infoFormEditor" class="layui-clear nowrap padding-bottom-30">
    <div class="layui-card news-right">
        <div class="layui-card-body">
            <!--<form onsubmit="return false" data-auto="true" action="{:request()->url()}" method="post" class='layui-form layui-card' autocomplete="off">-->
            <form class="layui-form padding-20" role="form" name="infoForm" onsubmit="return false">
                <label class="layui-form-item relative block">
                    <span class="color-green">企业名称</span>
                    <input maxlength="256" ng-model="item.name" required placeholder="请在这里输入企业名称" name='name' class="layui-input">
                </label>
                <label class="layui-form-item relative block">
                    <span class="color-green">企业简介</span>
                    <input maxlength="64" ng-model="item.intro" required placeholder="请在这里输入企业简介" name='intro' class="layui-input">
                </label>
                <div name="thumb-box" class="layui-form-item label-required-prev">
                    <span class="color-green">企业LOGO</span>
                    <div class="layui-clear">
                        <div class="upload-image-box pull-left transition" data-tips-image="{{item.logo}}" style="{{item.style}}">
                            <input ng-model="item.logo" data-auto-none value="{{item.logo}}" type="hidden" name="logo">
                        </div>
                        <div class="pull-left padding-top-15 padding-left-15">
                            <button type="button" data-title="上传LOGO" data-file="btn" data-type="jpg,png,jpeg" data-field="logo" class="layui-btn layui-btn-sm layui-btn-primary">上传LOGO</button>
                        </div>
                    </div>
                    <p class="color-desc">封面大图片建议尺寸 160像素 * 160像素</p>
                </div>
                <div class="layui-form-item label-required-prev">
                    <span class="color-green">图文文章内容</span>
                    <textarea ng-model="item.content" name='content'></textarea>
                </div>
                <div class="layui-form-item text-center padding-top-30">
                    <input ng-model="x.read_num" type="hidden">
                    <button ng-click="submit()" type="button" class="layui-btn">保存信息</button>
                </div>
            </form>
        </div>
    </div>
</div>
<div name="annex-template" style="display: none;">
    <div class="item" file="__URL__">
        <a target="_blank" href="__URL__">__TITLE__</a>
        <span class="font-s20 fa fa-trash"></span>
    </div>
</div>
{/block}


{block name='script'}
<script>window.form.render()</script>
<script>

    require(['angular', 'ckeditor'], function () {

        var editor;
        var $form = $('form[name="infoForm"]');
        var $vali = $form.vali().data('validate');

        var app = angular.module("infoFormEditor", []).run(callback);
        angular.bootstrap(document.getElementById(app.name), [app.name]);

        function callback($rootScope) {
            $rootScope.list = [];
            $rootScope.item = {};
            $.form.load('{:request()->url()}', {output: 'json'}, 'get', function (ret) {
                console.log(typeof ret);
                console.log(ret);
                return $rootScope.$apply(function () {
                    apply(ret.data || {});
                }), false;
            });
            function apply(qiye) {
                if(qiye.id==undefined) qiye = {
                    name: '', phone: '', content: '',intro:'',
                    addr: '', logo: '__ROOT__/static/theme/img/image.png'
                };
                $rootScope.item = qiye;
                $('.layui-card-body.layui-hide').removeClass('layui-hide');
                $rootScope.item.style = "background-image:url('" + $rootScope.item.logo + "')";
                setTimeout(function () {
                    if (editor) editor.destroy();
                    editor = window.createEditor('[name="content"]');
                    editor.setData($rootScope.item.content);
                    $vali.checkAllInput();
                }, 100);
            }
            $rootScope.setItemValue = function (name, value) {
                $rootScope.item[name] = value;
                $rootScope.item.style = "background-image:url('" + $rootScope.item.logo + "')";
            };

            $rootScope.setItemValuel = function (name, value) {
                $rootScope.item[name] = value;
                $rootScope.item.style = "background-file:url('" + $rootScope.item.annex + "')";
            };

            $rootScope.upItem = function (index, $event) {
                $event.stopPropagation();
                var tmp = [], cur = $rootScope.list[index];
                if (index < 1) return false;
                for (var i in $rootScope.list) {
                    (parseInt(i) === parseInt(index) - 1) && tmp.push(cur);
                    (parseInt(i) !== parseInt(index)) && tmp.push($rootScope.list[i]);
                }
                apply(tmp);
            };
            $rootScope.dnItem = function (index, $event) {
                $event.stopPropagation();
                var tmp = [], cur = $rootScope.list[index];
                if (index > $rootScope.list.length - 2) return false;
                for (var i in $rootScope.list) {
                    (parseInt(i) !== parseInt(index)) && tmp.push($rootScope.list[i]);
                    (parseInt(i) === parseInt(index) + 1) && tmp.push(cur);
                }
                apply(tmp);
            };
            $rootScope.delItem = function (index, $event) {
                $event.stopPropagation();
                var list = $rootScope.list, temp = [];
                for (var i in list) (parseInt(i) !== parseInt(index)) && temp.push(list[i]);
                apply(temp);
            };
            $rootScope.submit = function () {
                $vali.checkAllInput();
                if ($form.find('.validate-error').size() > 0) {
                    return $.msg.tips('表单验证不成功，请输入需要的内容！');
                }
                var data = {
                    id: $rootScope.item.id,
                    name: $rootScope.item.name,
                    intro: $rootScope.item.intro,
                    logo: $rootScope.item.logo,
                    content: editor.getData(),
                };
                $.form.load('{:request()->url()}', {data: data}, "post");
            };
            $('[name="logo"]').on('change', function (value) {
                value = this.value;
                $rootScope.$apply(function () {
                    $rootScope.setItemValue('logo', value);
                });
            });
        }

    });
</script>




{/block}